package com.genesis.yunnanji.utils;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

/**
 * 创建日期：2018/9/19 on 16:03
 * 作者:Kite
 * 描述:Android格式化手机号xxx xxxx xxxx
 */
public class PhoneUtil {

    private String mobile;

    public String phoneEditNum(final EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String contents = s.toString();
                int length = contents.length();
                if (length == 4) {
                    if (contents.substring(3).equals(new String(" "))) { // -
                        contents = contents.substring(0, 3);
                        editText.setText(contents);
                        editText.setSelection(contents.length());
                    } else { // +
                        contents = contents.substring(0, 3) + " " + contents.substring(3);
                        editText.setText(contents);
                        editText.setSelection(contents.length());
                    }
                } else if (length == 9) {
                    if (contents.substring(8).equals(new String(" "))) { // -
                        contents = contents.substring(0, 8);
                        editText.setText(contents);
                        editText.setSelection(contents.length());
                    } else {// +
                        contents = contents.substring(0, 8) + " " + contents.substring(8);
                        editText.setText(contents);
                        editText.setSelection(contents.length());
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                mobile = editText.getText().toString().trim();
                mobile = mobile.replace(" ", "");
                if (mobile.length() == 11) {

                } else {

                }
            }
        });
        return mobile;
    }
}
